package app.server.project.core.util;

import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDateTime;
import java.util.Date;

@Slf4j
public class SysTimeUtil {

    private static long startTime = System.currentTimeMillis();
    private static long appTimeLong = System.currentTimeMillis();
    private static LocalDateTime appTimeLocalTime = LocalDateTime.now();

    /**
     * 获取app时间,方便调整时间测试
     *
     * @return
     */
    public static long getAppTimeLong() {
        return appTimeLong + (System.currentTimeMillis() - startTime);
    }

    /**
     * 获取app时间,方便调整时间测试
     *
     * @return
     */
    public static LocalDateTime getAppTimeLocalTime() {
        return appTimeLocalTime.plusSeconds((System.currentTimeMillis() - startTime) / 1000);
    }

    /**
     * 获取app时间,方便调整时间测试
     *
     * @return
     */
    public static Date getAppDate() {
        var time = new Date(getAppTimeLong());
        return time;
    }

    /**
     * 设置系统时间
     *
     * @param date
     */
    public static void setAppTime(String date) {
        Date target = DateUtil.parse(date);
        startTime = System.currentTimeMillis();
        appTimeLong = target.getTime();
        appTimeLocalTime = DateUtil.toLocalDateTime(target);
        log.info("app系统时间:{},时间戳:{}", appTimeLocalTime, appTimeLong);
    }

    public static void reset() {
        startTime = System.currentTimeMillis();
        appTimeLong = System.currentTimeMillis();
        appTimeLocalTime = LocalDateTime.now();
        log.info("app系统时间:{},时间戳:{}", appTimeLocalTime, appTimeLong);
    }
}
